package com.raychi.demo.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.raychi.demo.dao.CompanyDao;
import com.raychi.demo.entity.Company;
import com.raychi.demo.util.JdbcUtil;

public class CompanyService {
	
	private CompanyDao companyDao;
	
	public List<Company> getCompanyList() throws ClassNotFoundException, SQLException {
		Connection conn = null;
		List<Company> list = null;
		try {
			conn = JdbcUtil.getConnection();
			list = companyDao.getCompanyList(conn);
			conn.commit();
		} catch (Exception e) {
			JdbcUtil.rollback(conn);
		} finally {
			JdbcUtil.closeConnection(conn);
		}
		return list;
	}
	
	public void update(Company company) throws Exception{
		Connection conn = null;
		try {
			conn = JdbcUtil.getConnection();
			companyDao.update(company,conn);
			conn.commit();
		} catch (Exception e) {
			JdbcUtil.rollback(conn);
		} finally {
			JdbcUtil.closeConnection(conn);
		}
	}
	
	public void delete(Company company) throws Exception{
		Connection conn = null;
		try {
			conn = JdbcUtil.getConnection();
			companyDao.delete(company, conn);
			conn.commit();
		} catch (Exception e) {
			JdbcUtil.rollback(conn);
		} finally {
			JdbcUtil.closeConnection(conn);
		}
	}
	
	public void save(Company company) throws Exception{
		Connection conn = null;
		try{
			conn = JdbcUtil.getConnection();
			companyDao.save(company, conn);
			conn.commit();
		}catch(Exception e){
			JdbcUtil.rollback(conn);
		}finally{
			JdbcUtil.closeConnection(conn);
		}
	}
	
	
	public void setCompanyDao(CompanyDao companyDao) {
		this.companyDao = companyDao;
	}

}
